package com.bilibili.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bilibili.dao.UserInfoMapper;
import com.bilibili.dao.VideoCommentMapper;
import com.bilibili.domain.PageResult;
import com.bilibili.domain.entity.UserInfo;
import com.bilibili.domain.entity.Video;
import com.bilibili.domain.entity.VideoComment;
import com.bilibili.domain.exception.ConditionException;
import com.bilibili.service.UserInfoService;
import com.bilibili.service.VideoCommentService;
import com.bilibili.service.VideoService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author www
* @description 针对表【t_video_comment(视频评论表)】的数据库操作Service实现
* @createDate 2024-06-17 21:04:45
*/
@Service
public class VideoCommentServiceImpl extends ServiceImpl<VideoCommentMapper, VideoComment>
    implements VideoCommentService {
    @Resource
    private VideoCommentMapper videoCommentMapper;

    @Resource
    private VideoService videoService;

    //TODO
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public void addVideoComment(VideoComment videoComment, Long userId) {
        Long videoId = videoComment.getVideoId();
        if (videoId==null){
            throw new ConditionException("参数异常！");
        }
        Video video = videoService.getById(videoId);
        if (video==null){
            throw new ConditionException("非法视频！");
        }
        videoComment.setUserId(userId);
        videoComment.setCreateTime(new Date());
        videoCommentMapper.insert(videoComment);
    }

    @Override
    public PageResult<VideoComment> pageListVideoComments(Integer size, Integer no, Long videoId) {
        Video video = videoService.getById(videoId);
        if (video==null){
            throw new ConditionException("非法视频！");
        }
        Map<String,Object> param = new HashMap<>();
        param.put("start",(no-1)*size);
        param.put("limit",size);
        param.put("videoId",videoId);

        Integer total = videoCommentMapper.pageCountVideoComments(param);
        List<VideoComment> list = new ArrayList<>();

        if (total>0){
            list = videoCommentMapper.pageListVideoComments(param);
            //批量查询二级评论
            List<Long> parentIdList = list.stream()
                    .map(VideoComment::getId)
                    .collect(Collectors.toList());
            List<VideoComment> childCommentList = videoCommentMapper.batchGetVideoCommentsByRootIds(parentIdList);
            //批量查询用户信息
            Set<Long> userIdList = list.stream()
                    .map(VideoComment::getUserId)
                    .collect(Collectors.toSet());

            Set<Long> replyUserIdList = list.stream()
                    .map(VideoComment::getReplyUserId)
                    .collect(Collectors.toSet());

            userIdList.addAll(replyUserIdList);
            List<UserInfo> userInfoList = userInfoMapper.selectByUserIds(userIdList);
            Map<Long,UserInfo> userInfoMap = userInfoList.stream()
                    .collect(Collectors.toMap(UserInfo::getUserId,userInfo -> userInfo));

            //遍历一级评论列表
            list.forEach(comment ->{
                Long id = comment.getId();
                List<VideoComment> childList = new ArrayList<>();
                //遍历二级评论
                childCommentList.forEach(child ->{
                    if (id.equals(child.getRootId())){
                        child.setUserInfo(userInfoMap.get(child.getUserId()));
                        child.setReplyUserInfo(userInfoMap.get(child.getReplyUserId()));
                        childList.add(child);
                    }
                });
                comment.setChildList(childList);
                comment.setUserInfo(userInfoMap.get(comment.getUserId()));
            });
        }
        return new PageResult<>(total,list);
    }
}




